package org.wyn.service.impl;

import java.util.List;

import org.wyn.mongodb.bean.UserDetail;
import org.wyn.mongodb.bean.VisitHistory;
import org.wyn.mongodb.dao.impl.UserDetailDao;
import org.wyn.mongodb.dao.impl.VisitHistoryDao;
import org.wyn.service.IProfileService;

import com.google.inject.Inject;

public class ProfileServiceImpl implements IProfileService{

	@Override
	public void visitOtherProfile(String hoster, String visitor) {
		if(null == hoster || null == visitor || hoster.equals(visitor))//自己访问自己"访问量","最近访问者"等信息不记录
			return;
		//历史总访问数量加一
		UserDetail ud = userDetailDao.findOne("userName", hoster);
		ud.setVisitCount(ud.getVisitCount()+1);
		userDetailDao.save(ud);
		
		VisitHistory vh = visitHistoryDao.findOne("userName",hoster);
		List<String> visitors = vh.getRecentVisitors();
		//先删除再放到列表首位置
		visitors.remove(visitor);
		visitors.add(0, visitor);
		//检查是否超过最大限度，若超过删除离现在时间最长的一次访问记录
		if(visitors.size() > VisitHistory.MAX_VISITORS_NUM)
			visitors.remove(visitors.size()-1);
		visitHistoryDao.save(vh);
	}
	@Override
	public UserDetail getUserDetailByUserName(String userName) {
		return userDetailDao.findOne("userName", userName);
	}
	@Inject
	private UserDetailDao userDetailDao;
	@Inject
	private VisitHistoryDao visitHistoryDao;
	public UserDetailDao getUserDetailDao() {
		return userDetailDao;
	}
	public void setUserDetailDao(UserDetailDao userDetailDao) {
		this.userDetailDao = userDetailDao;
	}
	public VisitHistoryDao getVisitHistoryDao() {
		return visitHistoryDao;
	}
	public void setVisitHistoryDao(VisitHistoryDao visitHistoryDao) {
		this.visitHistoryDao = visitHistoryDao;
	}
	
}
